package com.thread;

import com.thread.MyThreadPoolExecutor;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author ljj
 */
public class Test1 {
	public static void main(String[] args) throws Exception {
		MyThreadPoolExecutor pool = new MyThreadPoolExecutor(3, 5, 10, new ArrayBlockingQueue<>(2),
				new MyThreadPoolExecutor.CallerRunsPolicy());
		for (int i = 0; i < 8; i++) {
			System.out.println("添加任务" + i);
			pool.execute(new TestTask(i));
		}
		//System.out.println("main退出");
		
		TimeUnit.SECONDS.sleep(8);
		
		
		pool.execute(new TestTask(10086));
		
		//TimeUnit.SECONDS.sleep(8);
		
		pool.shutdownNow();
	}
	
	
	public static class TestTask implements Runnable {
		private int i;
		
		public TestTask(int i) {
			this.i = i;
		}
		
		@Override
		public void run() {
			
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
			}
			System.out.println("任务" + i + "结束,执行于 " + Thread.currentThread().getName());
		}
		
		@Override
		public String toString() {
			return "任务" + i;
		}
	}
}
